<template>
  <div class="container">
    <div style="border: 1px solid #ccc; z-index: 99999">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :default-config="toolbarConfig"
        :mode="mode"
      />
      <Editor
        v-model="html"
        style="height: 610px; overflow-y: hidden"
        :default-config="editorConfig"
        :mode="mode"
        @onCreated="onCreated"
        @onChange="onChange"
        @onMaxLength="maxHandler"
      />
    </div>
  </div>
</template>

<script>
import "@wangeditor/editor/dist/css/style.css";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
// import { getToken } from "@/utils/auth";
// import { getApiUrl } from "@/utils/utils";

export default {
  name: "IsEditor",
  components: {
    Editor,
    Toolbar,
  },
  props: {
    value: {
      type: String,
      default: "<p><br></p>",
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      editor: null,
      html: this.value,
      toolbarConfig: {
        toolbarKeys: [
          "bold",
          "underline",
          "italic",
          "color",
          "fontSize",
          "bgColor",
          "justifyLeft",
          "justifyRight",
          "justifyCenter",
          "justifyJustify",
          "insertLink",
          "headerSelect",
          "insertTable",
          "uploadImage",
          "fullScreen",
        ],
      },
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {
          uploadImage: {
            // server: getApiUrl() + "/file/uploadImg",
            fieldName: "file",
            // base64LimitSize: 5 * 1024, // 20k以下转为base64
            headers: {
              // Authorization: getToken(),
            },
          },
        },
        readOnly: this.readOnly,
        maxLength: 1500,
      },
      mode: "simple", // or 'simple'
      // mode: "default", // or 'simple'
    };
  },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时，及时销毁编辑器
  },

  // 第四步：定义方法
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },
    onChange(editor) {
      this.$emit("input", editor.getHtml());
    },
    maxHandler(editor) {
      this.$message({
        message: "最多输入1500字",
        type: "warning",
      });
    },
  },
};
</script>

<style></style>
